package com.wjy.icu.service.impl;

import com.wjy.icu.common.utils.bean.BeanUtils;
import com.wjy.icu.domain.*;
import com.wjy.icu.domain.vo.BluetoothVo;
import com.wjy.icu.mapper.EquitmentBluetoothMonitorMapper;
import com.wjy.icu.mapper.WdtMonitorDetailMapper;
import com.wjy.icu.mapper.WdtMonitoredMapper;
import com.wjy.icu.service.IBluetoochService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @author: wangjunyong
 * @date: 2021/11/1 11:49
 */
@Service
public class BluetoothServiceImpl implements IBluetoochService {
    @Resource
    private EquitmentBluetoothMonitorMapper equitmentBluetoothMonitorMapper;
    @Resource
    private WdtMonitoredMapper monitoredMapper;
    @Resource
    private WdtMonitorDetailMapper monitorDetailMapper;
    @Override
    public BluetoothVo findByEquitId(Integer equitId) {
        EquitmentBluetoothMonitorExample example = new EquitmentBluetoothMonitorExample();
        example.createCriteria().andEquitmentIdEqualTo(equitId);
        List<EquitmentBluetoothMonitor> equitmentBluetoothMonitors = equitmentBluetoothMonitorMapper.selectByExample(example);
        if (equitmentBluetoothMonitors.size()>0){
            EquitmentBluetoothMonitor equitmentBluetoothMonitor = equitmentBluetoothMonitors.get(0);
            BluetoothVo bluetoothVo = new BluetoothVo();
            BeanUtils.copyBeanProp(bluetoothVo,equitmentBluetoothMonitor);
            Integer monitorId = equitmentBluetoothMonitor.getMonitorId();
            WdtMonitored wdtMonitored = monitoredMapper.selectByPrimaryKey(monitorId);
            if (wdtMonitored==null){
                bluetoothVo.setState(0);
            }else {
                bluetoothVo.setState(wdtMonitored.getState());
            }
            WdtMonitorDetailExample detailExample = new WdtMonitorDetailExample();
            detailExample.createCriteria().andMonitorIdEqualTo(monitorId);
            detailExample.setOrderByClause("gmt_create desc");
            WdtMonitorDetail wdtMonitorDetail = monitorDetailMapper.selectOneByExample(detailExample);
            if (wdtMonitorDetail==null){
                bluetoothVo.setTemp(null);
            }else {
                bluetoothVo.setTemp(wdtMonitorDetail.getHighestTemperature().toString());
            }
            return bluetoothVo;
        }else {
            return null;
        }
    }

    @Override
    public BluetoothVo findByEquitIdTask(Integer equitId) {
        EquitmentBluetoothMonitorExample example = new EquitmentBluetoothMonitorExample();
        example.createCriteria().andEquitmentIdEqualTo(equitId);
        List<EquitmentBluetoothMonitor> equitmentBluetoothMonitors = equitmentBluetoothMonitorMapper.selectByExample(example);
        if (equitmentBluetoothMonitors.size()>0){
            EquitmentBluetoothMonitor equitmentBluetoothMonitor = equitmentBluetoothMonitors.get(0);
            BluetoothVo bluetoothVo = new BluetoothVo();
            BeanUtils.copyBeanProp(bluetoothVo,equitmentBluetoothMonitor);
            Integer monitorId = equitmentBluetoothMonitor.getMonitorId();
            WdtMonitoredExample example1 = new WdtMonitoredExample();
            example1.createCriteria().andIdEqualTo(monitorId).andMonitoredIdIsNull();
            WdtMonitored wdtMonitored = monitoredMapper.selectOneByExample(example1);
            if (wdtMonitored==null){
                return null;
            }else {
                bluetoothVo.setState(wdtMonitored.getState());
            }
            WdtMonitorDetailExample detailExample = new WdtMonitorDetailExample();
            detailExample.createCriteria().andMonitorIdEqualTo(monitorId);
            detailExample.setOrderByClause("gmt_create desc");
            WdtMonitorDetail wdtMonitorDetail = monitorDetailMapper.selectOneByExample(detailExample);
            if (wdtMonitorDetail==null){
                bluetoothVo.setTemp(null);
            }else {
                bluetoothVo.setTemp(wdtMonitorDetail.getHighestTemperature().toString());
            }
            return bluetoothVo;
        }else {
            return null;
        }
    }

    @Override
    public List<EquitmentBluetoothMonitor> selectAllBluetooth() {
        EquitmentBluetoothMonitorExample example = new EquitmentBluetoothMonitorExample();
        List<EquitmentBluetoothMonitor> equitmentBluetoothMonitors = equitmentBluetoothMonitorMapper.selectByExample(example);
        return equitmentBluetoothMonitors;
    }
}
